var loginFun = {}

loginFun.init = function (){

}
loginFun.login_mobile_open = function (el){
    $(el).parent().find("li").removeClass("is-active");
    $(el).addClass("is-active");
    $(".user-login .login_wx_open").hide();
    $(".user-login .login_mobile_open").show();
    $(".user-login .login_wx_mini").hide();
}

loginFun.login_wx_open = function (el){
    $(el).parent().find("li").removeClass("is-active");
    $(el).addClass("is-active");
    $(".user-login .login_mobile_open").hide();
    $(".user-login .login_wx_open").show();
    $(".user-login .login_wx_mini").hide();
}

loginFun.login_wx_mini = function (el){
    $(el).parent().find("li").removeClass("is-active");
    $(el).addClass("is-active");
    $(".user-login .login_mobile_open").hide();
    $(".user-login .login_wx_open").hide();
    $(".user-login .login_wx_mini").show();
    let uuid = generateUUID()

    let path = "pages/index/index?uuid="+uuid;

    $.ajax({
        url:"/api/front/createWxaCode/",
        dataType:"json",
        type:"get",
        //async:"true/false",
        data:{
            "path": path
        },
        success:function(ret){
            if(ret.data?.code == 200){
                $(el).removeAttr("disabled");
                $(".user-login .login_wx_mini").children(".qrcode").attr('src',ret.data.data)
                checkLogin(el,uuid)
            }
        },
        error:function(){
            $(el).removeAttr("disabled");
        }
    });
}

function checkLogin(el,uuid){
    $.ajax({
        url:"/api/front/checkLogin/",
        dataType:"json",
        type:"get",
        //async:"true/false",
        data:{
            "uuid": uuid
        },
        success:function(ret){
           let bool =  $(".user-login .login_wx_mini").is(":visible") 
            if(ret.data?.code != 200 && bool){
                setTimeout(()=>{
                    checkLogin(el,uuid)
                },2000)
            }else{
                // 处理登陆成功内容
                loginFun.get_user_info(el, ret.data.data.access_token)
            }
        },
        error:function(){
        }
    });
}

function generateUUID() {
    var d = new Date().getTime();
    if (typeof performance !== 'undefined' && typeof performance.now === 'function'){
      d += performance.now();
    }
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
      var r = (d + Math.random() * 16) % 16 | 0;
      d = Math.floor(d / 16);
      return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
}
  


loginFun.handleLoginSuccess = function (token){
    // 关闭登陆框
    clonse_login();

    // 保存token 到 缓存 7 天
    setUserCookie("token", token, 3600 * 24 * 7);

    // 处理登陆按钮
    handleLoginBtn();

    // 提示信息
    layer.msg("登录成功");
    window.location.reload();
}

loginFun.login_mobile_send_vcode = function (el){
    $(el).attr("disabled", true);
    $.ajax({
        url:"/api/front/sendMsg/"+$("#v-code-phone").val(),
        dataType:"json",
        type:"get",
        //async:"true/false",
        data:{},
        success:function(ret){
            $(el).removeAttr("disabled");
            if("发送短信成功" === ret.data.msg){
                buttonClickBan(el, 60);
            }
            layer.msg(ret.data.msg);
        },
        error:function(){
            $(el).removeAttr("disabled");
        }
    });
}
loginFun.login_mobile_submit = function (el){
    $(el).attr("disabled", true);
    $.ajax({
        url:"/api/front/mobileLogin",
        dataType:"json",
        type:"post",
        //async:"true/false",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify({
            phoneNo: $("#v-code-phone").val(),
            code: $("#v-code").val()
        }),
        success:function(ret){
            if(ret.data.code === 200
                && undefined !== ret.data.data && null !== ret.data.data){
                // 处理登陆成功内容
                loginFun.get_user_info(el, ret.data.data.access_token)
            }else {
                $(el).removeAttr("disabled");
                // 验证码错误
                layer.msg(ret.data.msg);
            }
        },
        error:function(){
            $(el).removeAttr("disabled");
        }
    });
}
loginFun.get_user_info = function (el, token){
    $.ajax({
        url:"/api/front/customer/info/"+token,
        dataType:"json",
        type:"get",
        //async:"true/false",
        data:{},
        success:function(ret){
            $(el).removeAttr("disabled");
            if(ret.data.code === 200){
                localStorage.setItem("userInfo", JSON.stringify(ret.data.data));

                // 处理登陆成功内容
                loginFun.handleLoginSuccess(token);
            }else {
                // 验证码错误
                layer.msg("获取用户身份失败");
            }
        },
        error:function(){
            $(el).removeAttr("disabled");
            // 验证码错误
            layer.msg("获取用户身份失败");
        }
    });
}

loginFun.logout = function (){
    clearUserCookie("token");
    localStorage.removeItem("userInfo");
    window.location.reload();
}

loginFun.init();
